package com.uenpay.im.ui.old.activity.company;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qyx.android.message.type.PacketType;
import com.uenpay.im.R;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.weight.RoundImageView;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-2-22 13:02:55
 * @copyRight http://www.qiyunxin.com
 */
public class EmployeeAdapter extends BaseAdapter {

	private ArrayList<EmployeeEntity> mArrayList = null;
	private LayoutInflater mLayoutInflater = null;
	private boolean is_editor = false;

	public EmployeeAdapter(Context _Context,
			ArrayList<EmployeeEntity> _ArrayList, boolean _is_editor) {
		mLayoutInflater = LayoutInflater.from(_Context);
		mArrayList = _ArrayList;
		is_editor = _is_editor;
	}

	@Override
	public int getCount() {
		return mArrayList == null ? 0 : mArrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mArrayList == null ? null : mArrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		EmployeeViewHodler mEmployeeViewHodler = null;
		final EmployeeEntity mEmployeeEntity = mArrayList.get(arg0);
		if (arg1 == null) {
			arg1 = mLayoutInflater.inflate(R.layout.item_employee_layout, arg2,
					false);
			mEmployeeViewHodler = new EmployeeViewHodler();
			mEmployeeViewHodler.nameTv = (TextView) arg1
					.findViewById(R.id.name_tv);
			mEmployeeViewHodler.position_tv = (TextView) arg1
					.findViewById(R.id.position_tv);
			mEmployeeViewHodler.avatarIv = (RoundImageView) arg1
					.findViewById(R.id.avatar_iv);
			mEmployeeViewHodler.editIv = (ImageView) arg1
					.findViewById(R.id.edit_iv);
			mEmployeeViewHodler.selected_iv = (ImageView) arg1
					.findViewById(R.id.selected_iv);
			arg1.setTag(mEmployeeViewHodler);
		} else {
			mEmployeeViewHodler = (EmployeeViewHodler) arg1.getTag();
		}
		mEmployeeViewHodler.nameTv.setText(mEmployeeEntity.nickname);
		mEmployeeViewHodler.position_tv.setText(mEmployeeEntity.position);
		mEmployeeViewHodler.avatarIv
				.SetUrl(APIConfiguration.getAvatarUrlNormal(
						mEmployeeEntity.custid, PacketType.PERSONAL));
		if (is_editor) {
			mEmployeeViewHodler.editIv.setVisibility(View.VISIBLE);
		} else {
			mEmployeeViewHodler.editIv.setVisibility(View.GONE);
		}

		if (mEmployeeEntity.is_selected == -1) {
			mEmployeeViewHodler.selected_iv.setVisibility(View.GONE);
		} else {
			mEmployeeViewHodler.selected_iv.setVisibility(View.VISIBLE);
			if (mEmployeeEntity.is_selected == 1) {
				mEmployeeViewHodler.selected_iv
						.setImageResource(R.drawable.icon_checked);
			} else {
				mEmployeeViewHodler.selected_iv
						.setImageResource(R.drawable.icon_uncheck);
			}
		}
		return arg1;
	}

	private class EmployeeViewHodler {
		private TextView nameTv, position_tv;
		private RoundImageView avatarIv;
		private ImageView editIv, selected_iv;
	}

	/**
	 * @param custId
	 */
	public void setSelected(String custId) {
		if (mArrayList != null) {
			for (int i = 0, size = mArrayList.size(); i < size; i++) {
				if (mArrayList.get(i).custid.equals(custId)) {
					mArrayList.get(i).is_selected = 1;
				} else {
					mArrayList.get(i).is_selected = 0;
				}
			}
			notifyDataSetChanged();
		}
	}

	public EmployeeEntity getSelectedUser() {
		EmployeeEntity mEmployeeEntity = null;
		for (int i = 0, size = mArrayList.size(); i < size; i++) {
			if (mArrayList.get(i).is_selected == 1) {
				mEmployeeEntity = mArrayList.get(i);
				break;
			}
		}
		return mEmployeeEntity;
	}

}
